﻿// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Copyright (c) 2011, Dr. Masroor Ehsan
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the author nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// $Author:$
// $Id:$
// $Rev:$
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

using System;

namespace MaxTrader.Core
{
    public sealed class TradingTime : IComparable<TradingTime>, ICloneable, IParameterizedInstance<TradingTime, DateTime>
    {
        /** Number of hours in one day. */
        public const int HoursInDay = 24;

        /** Number of minutes in one hour. */
        public const int MinutesInHour = 60;

        /** Number of seconds in one minute. */
        public const int SecondsInMinute = 60;

        /** Number of milliseconds in one second. */
        public const int MillisecondsInSecond = 1000;

        // Hour from 0 to 23
        private readonly int _hour;

        // Minute from 0 to 59
        private readonly int _minute;

        // Second from 0 to 62 (leap seconds)
        private readonly int _second;

        /// <summary>
        ///   Create a new time from the given hour, minute and second.
        /// </summary>
        /// <param name = "hour">the hour from 0 to 23</param>
        /// <param name = "minute">the minute from 0 to 59</param>
        /// <param name = "second">the second from 0 to 62 (leap seconds)</param>
        public TradingTime(int hour, int minute, int second)
        {
            _hour = hour;
            _minute = minute;
            _second = second;
        }

        /// <summary>
        ///   Create a new time from the specified date-time
        /// </summary>
        public TradingTime(DateTime time) : this(time.Hour, time.Minute, time.Second)
        {
        }

        /// <summary>
        ///   Create a new time set to now
        /// </summary>
        public TradingTime() : this(DateTime.UtcNow.Hour, DateTime.UtcNow.Minute, DateTime.UtcNow.Second)
        {
        }

        public int Hour
        {
            get { return _hour; }
        }

        public int Minute
        {
            get { return _minute; }
        }

        public int Second
        {
            get { return _second; }
        }

        #region ICloneable Members

        public object Clone()
        {
            return new TradingTime(_hour, _minute, _second);
        }

        #endregion

        #region IComparable<TradingTime> Members

        /// <summary>
        ///   Compare the current time to the specified time.
        /// </summary>
        /// <param name = "other">the time to compare</param>
        /// <returns>
        ///   -1: this time is before the specified time
        ///   0:  the times are equal
        ///   1:  this time is after the specified time
        /// </returns>
        public int CompareTo(TradingTime other)
        {
            if (_hour < other.Hour) return -1;
            if (_hour > other.Hour) return 1;

            if (_minute < other.Minute) return -1;
            if (_minute > other.Minute) return 1;

            if (_second < other.Second) return -1;
            if (_second > other.Second) return 1;

            return 0;
        }

        #endregion

        public DateTime Time
        {
            get { return DateTime.SpecifyKind(new DateTime(0, 0, 0, Hour, Minute, Second), DateTimeKind.Utc); }
        }

        /// <summary>
        ///   Tests if this time is after the specified time
        /// </summary>
        /// <param name = "other">the specified time to compare</param>
        /// <returns>true if the specified time is after this one</returns>
        public bool After(TradingTime other)
        {
            return CompareTo(other) > 0;
        }

        /// <summary>
        ///   Tests if this time is before the specified time
        /// </summary>
        /// <param name = "other">the specified time to compare</param>
        /// <returns>true if the specified time is before this one</returns>
        public bool Before(TradingTime other)
        {
            return CompareTo(other) < 0;
        }

        /// <summary>
        ///   Tests if this time is equal to the specified time
        /// </summary>
        /// <param name = "other">the specified time to compare</param>
        /// <returns>true if the specified time is equal</returns>
        public bool Equals(TradingTime other)
        {
            return CompareTo(other) == 0;
        }

        /// <summary>
        ///   Calculates the difference between this and another TradingTime in seconds
        /// </summary>
        /// <returns>the difference between this and another TradingTime in seconds</returns>
        public int Diff(TradingTime other)
        {
            return ((other.Hour - _hour)*MinutesInHour*SecondsInMinute +
                    (other.Minute - _minute)*SecondsInMinute +
                    (other.Second - _second));
        }

        public static TradingTime CreateInstance(DateTime param)
        {
            return new TradingTime(param);
        }

        TradingTime IParameterizedInstance<TradingTime, DateTime>.CreateInstance(DateTime param)
        {
            return CreateInstance(param);
        }
    }
}